<?php

namespace App\Listeners\UserRecommend;

use App\Common\Libs\Dispatch\Console;
use App\Console\Commands\Controller\UserRecommendCommand;
use App\Consts\GlobalConsoleConst;
use App\Consts\GlobalDynamicConst;
use App\Events\UserRecommend\CreateUserRecommendEvent;
use App\Listeners\BaseListener;

class CreateUserRecommendListener extends BaseListener
{
    /**
     * Handle the event.
     *
     * @param CreateUserRecommendEvent $event
     * @return bool
     */
    public function handle(CreateUserRecommendEvent $event)
    {
        $this->saveFileLog(__CLASS__, $event);
        $userRecommend = $event->userRecommend;

        // 修改推荐卡片用户头像区域
        Console::getInstance(UserRecommendCommand::class)
            ->push([
                'target_id' => $userRecommend['target_id']
            ], GlobalConsoleConst::USER_RECOMMEND_AVATAR_AREA);

        // 队列记录日志
        service()->LogCountService->createLogByQueue($event->logCountLoadData);

        // 新增动态，延迟三秒，防止重复生成动态
        service()->DynamicService->addDynamicByTarget([
            'user_id' => $userRecommend['user_id'],
            'target_id' => $userRecommend['target_id'],
            'target_type' => $userRecommend['target_type'],
            'type' => GlobalDynamicConst::DYNAMIC_TYPE_USER_RECOMMEND_ARTICLE
        ], 3);

        return true;
    }
}
